<template>
  <div>
    <button @click="navigateToJenkinsHome">Go to Jenkins Home</button>
  </div>
</template>

<script>

export default {
  name: 'NavigateToJenkins',
  methods: {
    // async navigateToJenkinsHome() {
    //   // 获取 Cookies 和 CSRF 令牌
    //   const response = await fetch('/jenkins/j_spring_security_check', {
    //     method: 'POST',
    //     headers: {
    //       'Content-Type': 'application/x-www-form-urlencoded'
    //     },
    //     body: new URLSearchParams({
    //       'j_username': 'admin',
    //       'j_password': 'p@ssw0rd',
    //       'from': '/'
    //     }).toString()
    //   })

    //   if (response.ok) {
    //     window.open('http://192.168.94.1:18080/', '_blank')
    //   } else {
    //     console.error('Login failed')
    //   }
    // }
    // async navigateToJenkinsHome() {
    //   // 获取 Cookies 和 CSRF 令牌
    //   const response = await fetch('/api/jenkins-login', {
    //     method: 'GET',
    //     redirect: 'manual'
    //   })

    //   console.log(response)
    //   debugger

    //   if (response.status === 0) {
    //     window.open('http://192.168.94.1:18080/', '_blank')
    //   } else {
    //     console.error('jenkins跳转失败!')
    //   }
    // }

    navigateToJenkinsHome() {
      // 创建隐藏表单
      const form = document.createElement('form')
      form.method = 'POST'
      form.action = 'http://192.168.94.1:18080/j_spring_security_check'
      form.target = '_blank'

      // 添加表单字段
      const usernameField = document.createElement('input')
      usernameField.type = 'hidden'
      usernameField.name = 'j_username'
      usernameField.value = 'admin'
      form.appendChild(usernameField)

      const passwordField = document.createElement('input')
      passwordField.type = 'hidden'
      passwordField.name = 'j_password'
      passwordField.value = 'p@ssw0rd'
      form.appendChild(passwordField)

      const fromField = document.createElement('input')
      fromField.type = 'hidden'
      fromField.name = 'from'
      fromField.value = '/'
      form.appendChild(fromField)

      // 将表单添加到文档并提交
      document.body.appendChild(form)
      form.submit()
      document.body.removeChild(form)
    }
  }
}
</script>
